home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / appsrcs.zip / APPFINDR.ZIP / APPFINDR.C < prev    next >
C/C++ Source or Header  |  1993-04-29  |  32KB  |  984 lines

  1. /*-------------------------------------------------------------------------
  2.  AppFindr.c
  3.  
  4.  AppFinder, a File launch utility for AppBar.
  5.  
  6.  by
  7.  GMP van kempen
  8.  NEVERnever Software 1993
  9.  
  10. History:
  11. 4.00.1 First Beta release
  12.  
  13.  
  14. ---------------------------------------------------------------------------*/
  15. #define STRICT
  16. #include <windows.h>
  17. #include <windowsx.h>
  18. #include <shellapi.h>
  19. #include <ctype.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <io.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <direct.h>
  26. #include "appfindr.h"
  27.  
  28. HANDLE hFont;
  29. HINSTANCE hInst;
  30. HMENU  hMenu;
  31. HWND   ghWnd;
  32. HWND   hWndFile, hWndDir, hWndDrive;
  33. HICON  hFileIcon, hExeFileIcon, hSmallIcon, hDosFileIcon, hTTFFileIcon;
  34. HICON  hATMFileIcon, hPIFFileIcon, hDrvFileIcon, hFontIcon, hEnhIcon;
  35. HICON  hFolderIcon, hDosFolderIcon, hSysFolderIcon;
  36. BOOL  FirstMinMax = TRUE;
  37.  
  38. char  szAppName[]    = "AppFindr";
  39. char  szAppTitle[]   = "AppFinder";
  40. char  DropFile[80], Temp[80];
  41. char  DriveName[10], PathName[90];
  42.  
  43. int   CharWidth, CharHeight;
  44. int   xSpacing, ySpacing, xSelPos = 0, ySelPos = 0;
  45. int   xMaxIcons, yMaxIcons;
  46. int   IconsInWindow, FileCount, DirCount, DriveCount;
  47. int   CurrentIndex, CurrentSelection = 0;
  48.  
  49. RECT  gClientRect;
  50.  
  51. /* functions for Drag & Drop serving */
  52. HGLOBAL FAR DragCreateFiles (LPPOINT lpptMousePos, BOOL fInNonClientArea);
  53. HGLOBAL FAR DragAppendFile (HGLOBAL hDrop, LPCSTR szPathname);
  54.  
  55. /*----------------------------------------------------------------------------*/
  56. /* FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)                              */
  57. /*                                                                            */
  58. /* PURPOSE:  Calls initialization function, processes message loop            */
  59. /*----------------------------------------------------------------------------*/
  60. int PASCAL WinMain(HINSTANCE Inst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
  61.     {
  62.     MSG  msg;
  63.     int xScreen, yScreen;
  64.  
  65.     if (!hPrevInst)
  66.         {
  67.         if(!AppInit(Inst))
  68.             return(FALSE);
  69.         }
  70.     else
  71.         return(FALSE);
  72.  
  73.     /* get screen size to determine Window position */
  74.     xScreen = GetSystemMetrics(SM_CXSCREEN);
  75.     yScreen = GetSystemMetrics(SM_CYSCREEN);
  76.  
  77.     ghWnd = CreateWindow(szAppName, szAppTitle,
  78.          WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_VSCROLL,
  79.          xScreen/4, yScreen/4, CW_USEDEFAULT, 0, NULL, NULL, Inst, NULL);
  80.  
  81.     if (!ghWnd)
  82.     return (FALSE);
  83.  
  84.     /* load all file and directory icons */
  85.     hFileIcon = LoadIcon(hInst, "FileIcon");
  86.     hDosFileIcon = LoadIcon(hInst, "DosFileIcon");
  87.     hExeFileIcon = LoadIcon(hInst, "ExeFileIcon");
  88.     hPIFFileIcon = LoadIcon(hInst, "PIFFileIcon");
  89.     hATMFileIcon = LoadIcon(hInst, "ATMFileIcon");
  90.     hTTFFileIcon = LoadIcon(hInst, "TTFFileIcon");
  91.     hDrvFileIcon = LoadIcon(hInst, "DrvFileIcon");
  92.     hEnhIcon = LoadIcon(hInst, "EnhIcon");
  93.     hFontIcon = LoadIcon(hInst, "FontIcon");
  94.     hSmallIcon = LoadIcon(hInst, "SmallIcon");
  95.     hFolderIcon = LoadIcon(hInst, "FolderIcon");
  96.     hSysFolderIcon = LoadIcon(hInst, "SysFolderIcon");
  97.     hDosFolderIcon = LoadIcon(hInst, "DosFolderIcon");
  98.  
  99.     ShowWindow(ghWnd, nCmdShow);
  100.     UpdateWindow(ghWnd);
  101.  
  102.     while (GetMessage(&msg, NULL, NULL, NULL))
  103.     {
  104.     TranslateMessage(&msg);
  105.     DispatchMessage(&msg);
  106.         }
  107.  
  108.     return (msg.wParam);
  109.     }
  110.  
  111. /*----------------------------------------------------------------------------*/
  112. /* FUNCTION: AppInit(HANDLE)                                                  */
  113. /*                                                                            */
  114. /* PURPOSE: Initializes window data and registers window class                */
  115. /*----------------------------------------------------------------------------*/
  116. BOOL AppInit(HINSTANCE hInstance)
  117.     {
  118.     HDC hDC;
  119.     WNDCLASS   rClass;
  120.     TEXTMETRIC tm;
  121.     HFONT hOldFont;
  122.     int     cxIcon = GetSystemMetrics (SM_CXICON);
  123.  
  124.     hInst = hInstance;
  125.  
  126.     /* get char width and height */
  127.     hDC = GetDC(ghWnd);
  128.     hOldFont = SelectObject (hDC, GetStockObject (ANSI_VAR_FONT));
  129.     GetTextMetrics(hDC,&tm);
  130.     CharWidth  = tm.tmAveCharWidth;
  131.     CharHeight = tm.tmHeight + tm.tmExternalLeading;
  132.     xSpacing = (15 * CharWidth);
  133.     if(xSpacing < (2 * cxIcon))
  134.     xSpacing = 2 * cxIcon;
  135.     ySpacing  = (CharHeight + 49);
  136.     SelectObject (hDC, hOldFont);
  137.     ReleaseDC (ghWnd,hDC);
  138.  
  139.  
  140.     rClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  141.     rClass.hIcon     = LoadIcon(hInstance, "SysFolderIcon");
  142.     rClass.lpszMenuName  = NULL;
  143.     rClass.lpszClassName = szAppName;
  144.     rClass.hbrBackground = GetStockObject(WHITE_BRUSH);
  145.     rClass.hInstance     = hInstance;
  146.     rClass.style     = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  147.     rClass.lpfnWndProc   = AppWndProc;
  148.  
  149.     return RegisterClass(&rClass);
  150.     }
  151.  
  152. /*----------------------------------------------------------------------------*/
  153. /* FUNCTION: AppWndProc(HWND, unsigned, WORD, LONG)                           */
  154. /*                                                                            */
  155. /* PURPOSE:  Processes messages for main window                               */
  156. /*----------------------------------------------------------------------------*/
  157. long FAR PASCAL AppWndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam)
  158.     {
  159.     PAINTSTRUCT ps;
  160.     HFONT hOldFont;
  161.     HDC hDC;
  162.     int xMousePos, xPos, yMousePos, yPos, length;
  163.     char Name[80], DirName[80], szDropPathName[200];
  164.     HCURSOR hCrsrDrpNotAllow, hCrsrDrpSingle;
  165.     BOOL fCallDefProc = FALSE, fInNonClientArea, fOkToDrop;
  166.     LONG lResult = 0;
  167.     POINT ptMousePos;
  168.     HWND hWndSubject;
  169.     HGLOBAL hDrop, hDropT;
  170.  
  171.     switch (message)
  172.         {
  173.     case WM_CREATE:
  174.         /* create menu for AppFinder, on this menu all drives will
  175.         be placed */
  176.         hMenu = CreateMenu(hWnd);
  177.         /* create hidden listbox windows from which the listed drives,
  178.            directories and files are read */
  179.         hWndFile = CreateWindow("listbox", NULL, WS_CHILD | LBS_SORT,
  180.                     CharWidth, CharHeight,
  181.                     CharWidth * 16, CharHeight * 20,
  182.                     hWnd, IDC_INVISIBLE, hInst, NULL);
  183.         hWndDir = CreateWindow("listbox", NULL, WS_CHILD | LBS_SORT,
  184.                     CharWidth, CharHeight,
  185.                     CharWidth * 16, CharHeight * 20,
  186.                     hWnd, IDC_INVISIBLE+1, hInst, NULL);
  187.         hWndDrive = CreateWindow("listbox", NULL, WS_CHILD | LBS_SORT,
  188.                     CharWidth, CharHeight,
  189.                     CharWidth * 16, CharHeight * 20,
  190.                     hWnd, IDC_INVISIBLE+2, hInst, NULL);
  191.         SetScrollRange(hWnd, SB_VERT, 0, 0, FALSE);
  192.         /* place all drives on the menu */
  193.         MakeDriveList();
  194.         /* make the create menau AppFinder's menu */
  195.         SetMenu(hWnd, hMenu);
  196.         /* fill the directory and drive listboxes */
  197.         MakeFileList();
  198.  
  199.         DragAcceptFiles(hWnd, TRUE);
  200.         break;
  201.  
  202.     case WM_GETMINMAXINFO:
  203.             if(FirstMinMax)
  204.                 {                      // Set maximum size for opening window
  205.                 ((LPPOINT)lParam+4)->x = (xSpacing * 6)+20;
  206.                 ((LPPOINT)lParam+4)->y = (ySpacing * 4);
  207.                 FirstMinMax = FALSE;
  208.                 }
  209.             else                       // ensure enough room for at least 1 icon
  210.                 {
  211.                 ((LPPOINT)lParam+3)->x = (xSpacing)+20;
  212.                 ((LPPOINT)lParam+3)->y = (ySpacing * 2);
  213.                 }
  214.         break;
  215.  
  216.     case WM_VSCROLL:
  217.             VerticalScrollProcess(wParam, lParam);
  218.             break;
  219.  
  220.     case WM_SIZE:
  221.         if(wParam != SIZEICONIC)
  222.                 {
  223.                 int xTmp, yTmp;
  224.  
  225.                 GetClientRect (ghWnd,&gClientRect);
  226.                 xTmp = (4+ gClientRect.right  - gClientRect.left) / xSpacing;
  227.                 yTmp = (   gClientRect.bottom - gClientRect.top)  / ySpacing;
  228.  
  229.                 gClientRect.bottom = gClientRect.top + (ySpacing * yTmp);
  230.  
  231.                 if(xTmp != xMaxIcons || yTmp != yMaxIcons)
  232.                     {
  233.                     xMaxIcons = xTmp;
  234.                     yMaxIcons = yTmp;
  235.             IconsInWindow = xTmp * yTmp;
  236.                     if(CurrentIndex > 0)            /* force index to boundry */
  237.                         CurrentIndex -= (CurrentIndex % IconsInWindow);
  238.                     AdjustScrollBar();
  239.                     }
  240.                 }
  241.             break;
  242.  
  243.     case WM_PAINT:
  244.         hDC = BeginPaint(hWnd,&ps);
  245.         hOldFont = SelectObject(hDC, GetStockObject (ANSI_VAR_FONT));
  246.         ViewIcons(hDC);
  247.         SelectObject(hDC, hOldFont);
  248.         EndPaint(hWnd, &ps);
  249.             break;
  250.  
  251.     case WM_COMMAND:
  252.         /* WM_COMMAND message only generated when a drive on the
  253.         menu is choosen: change current drive to that drive, and
  254.         update directory and file listboxes */
  255.         if((wParam >= IDM_DRIVE) && (wParam < IDM_DRIVE+DriveCount))
  256.         {
  257.         ListBox_GetText(hWndDrive, wParam-IDM_DRIVE, DriveName);
  258.         _chdrive((DriveName[2]+1-'a'));
  259.         MakeFileList();
  260.         InvalidateRect(hWnd, NULL, TRUE);
  261.         }
  262.         break;
  263.  
  264.     case WM_LBUTTONDOWN:
  265.         /* if left mouse button is pressed, a new dir or file is
  266.            made the current selection */
  267.  
  268.         /* unselected the previous selection */
  269.         UpdateCurrentText(0);
  270.         /* if there are more then one files or directories calcutate
  271.            which is the new one */
  272.         if(FileCount > 1)
  273.         {
  274.         /* get mouse position at left button click */
  275.         xMousePos = (int) LOWORD(lParam);
  276.         yMousePos = (int) HIWORD(lParam);
  277.  
  278.         /* determine which possible icon position this would be */
  279.         xPos = (xMousePos-4)/xSpacing;
  280.         yPos = yMousePos/ySpacing;
  281.  
  282.         /* if the possible icon position doesn't actual have an icon
  283.            make the last icon the selected icon */
  284.         if(xPos > xMaxIcons)
  285.             xPos = xMaxIcons;
  286.         if(yPos > yMaxIcons)
  287.             yPos = yMaxIcons;
  288.  
  289.         /* update the global parameters xSelPos & ySelPos */
  290.         xSelPos = xPos;
  291.         ySelPos = yPos;
  292.  
  293.         /* calculate the Currentselection in terms of the postion
  294.            in the file and directory listbox from the selected icon
  295.            and the scrollbar offset (= CurrentIndex ) */
  296.         CurrentSelection = CurrentIndex + xMaxIcons*yPos+xPos;
  297.         if(CurrentSelection > FileCount)
  298.             {
  299.             CurrentSelection = CurrentIndex;
  300.             xSelPos = 0;
  301.             ySelPos = 0;
  302.             }
  303.         }
  304.         else
  305.         {
  306.         CurrentSelection = 0;
  307.         xSelPos = 0;
  308.         ySelPos = 0;
  309.         }
  310.         /* highlight the text of the current selected icon */
  311.         UpdateCurrentText(1);
  312.         return 0L;
  313.  
  314.     case WM_MOUSEMOVE:
  315.         /* if the left mousebutton is not down, there is no drag&drop */
  316.         if(wParam != MK_LBUTTON)
  317.         return 0L;
  318.  
  319.         /* no directories can be drag&dropped */
  320.         if(CurrentSelection < DirCount)
  321.         return 0L;
  322.  
  323.         // Get Name of DropFile
  324.         ListBox_GetText(hWndFile, CurrentSelection-DirCount, Temp);
  325.         strcpy(szDropPathName, PathName);
  326.         length = strlen(szDropPathName);
  327.         if(szDropPathName[length-1] != '\\')
  328.         strcat(szDropPathName, "\\");
  329.         strcat(szDropPathName, Temp);
  330.  
  331.         // User has initiated the "Drag and Drop" sequence
  332.         // Get the handles to the cursors that will shown to the user.
  333.         hCrsrDrpNotAllow = LoadCursor(hInst,"DRPFIL_NOTALLOWED");
  334.         hCrsrDrpSingle = LoadCursor(hInst,"DRPFIL_SINGLE");
  335.  
  336.         // *** Loop for determining the drop-file
  337.         //       client window ***
  338.         SetCapture(hWnd);
  339.         do    {
  340.         // Get cursor pos. & window under the cursor
  341.         GetCursorPos(&ptMousePos);
  342.         hWndSubject = WindowFromPoint(ptMousePos);
  343.  
  344.         if(!IsWindow(hWndSubject) ||
  345.              !(GetWindowLong(hWndSubject, GWL_EXSTYLE) &
  346.              WS_EX_ACCEPTFILES))
  347.             {
  348.             fOkToDrop = FALSE;
  349.             SetCursor(hCrsrDrpNotAllow);
  350.             }
  351.         else
  352.             {
  353.             fOkToDrop = TRUE;
  354.             SetCursor(hCrsrDrpSingle);
  355.             }
  356.  
  357.         // Terminate loop when mouse button is released
  358.         } while (GetAsyncKeyState(VK_LBUTTON) & 0x8000);
  359.         ReleaseCapture();
  360.  
  361.         // Free the loaded cursors from memory
  362.         DestroyCursor(hCrsrDrpNotAllow);
  363.         DestroyCursor(hCrsrDrpSingle);
  364.  
  365.         if (!fOkToDrop) return 0L;
  366.  
  367.         // Is the cursor in the window's non-client area?
  368.         fInNonClientArea = (HTCLIENT !=
  369.             SendMessage(hWndSubject, WM_NCHITTEST, 0,
  370.             (LONG) MAKELONG(ptMousePos.x, ptMousePos.y)));
  371.  
  372.         // Create drop-file memory block and initialize it
  373.         ScreenToClient(hWndSubject, &ptMousePos);
  374.         hDrop = DragCreateFiles(&ptMousePos, fInNonClientArea);
  375.  
  376.         // Append pathname to end of drop-file memory block
  377.         hDropT = DragAppendFile(hDrop, szDropPathName);
  378.  
  379.         if (hDropT == NULL)
  380.         {
  381.         GlobalFree(hDrop);
  382.         hDrop = NULL;
  383.         return 0L;    // Terminate while loop
  384.         }
  385.         else
  386.         hDrop = hDropT;
  387.  
  388.         if (hDrop != NULL)
  389.         {
  390.         // All pathnames appended successfully, post the message
  391.         // to the drop-file client window
  392.         PostMessage(hWndSubject, WM_DROPFILES, hDrop, 0L);
  393.  
  394.         // Don't free the memory, the Dropfile client will do it
  395.         }
  396.         return 0L;
  397.  
  398.     case WM_LBUTTONDBLCLK:
  399.         // leftmouse double click to change directory or launch a file
  400.         SetCursor(LoadCursor(NULL, IDC_WAIT));
  401.  
  402.         /* if a directory is double clicked, change to that directory
  403.            and update dir & file lists */
  404.         if(CurrentSelection < DirCount)
  405.         {
  406.         ListBox_GetText(hWndDir, CurrentSelection, Name);
  407.         GetRealDirName(Name, DirName);
  408.         chdir(DirName);
  409.         MakeFileList();
  410.         InvalidateRect(ghWnd,NULL,TRUE);
  411.         }
  412.         else
  413.         /* otherwise launch (associated) file */
  414.         {
  415.         ListBox_GetText(hWndFile, CurrentSelection-DirCount, Name);
  416.         ShellExecute(ghWnd, "open", Name, NULL, NULL, SW_SHOWNORMAL);
  417.         }
  418.         SetCursor(LoadCursor(NULL, IDC_ARROW));
  419.         return 0L;
  420.  
  421.     case WM_DESTROY:
  422.         DestroyWindow(hWndFile);
  423.         DestroyWindow(hWndDir);
  424.         DestroyWindow(hWndDrive);
  425.         DragAcceptFiles(hWnd, FALSE);
  426.         PostQuitMessage(0);
  427.             break;
  428.  
  429.     default:
  430.             return (DefWindowProc(hWnd, message, wParam, lParam));
  431.         } /* end switch */
  432.     return (NULL);
  433.     }
  434.  
  435. /*----------------------------------------------------------------------------*/
  436. /* FUNCTION: VerticalScrollProcess(WORD wParam, LONG lParam)                  */
  437. /*                                                                            */
  438. /* PURPOSE:  Processes WM_VSCROLL messages from main window                   */
  439. /*----------------------------------------------------------------------------*/
  440. void VerticalScrollProcess(WPARAM wParam, LPARAM lParam)
  441.     {
  442.     switch(wParam)
  443.         {
  444.         case SB_THUMBPOSITION:
  445.             CurrentIndex = LOWORD(lParam);
  446.             if(CurrentIndex > 0)                    /* force index to boundry */
  447.                 CurrentIndex -= (CurrentIndex % IconsInWindow);
  448.             InvalidateRect(ghWnd, NULL, TRUE);
  449.             break;
  450.  
  451.         case SB_TOP:
  452.             if(CurrentIndex > 0)
  453.                 {
  454.                 CurrentIndex = 0;
  455.                 InvalidateRect(ghWnd, NULL, TRUE);
  456.                 }
  457.             break;
  458.  
  459.         case SB_BOTTOM:
  460.             if(CurrentIndex + IconsInWindow < FileCount)
  461.                 {
  462.                 CurrentIndex = FileCount - IconsInWindow;
  463.                 InvalidateRect(ghWnd, NULL, TRUE);
  464.                 }
  465.             break;
  466.  
  467.         case SB_LINEUP:
  468.             if(CurrentIndex > 0)
  469.                 {
  470.                 CurrentIndex -= xMaxIcons;
  471.                 ScrollWindow(ghWnd, 0, ySpacing,
  472.                   (LPRECT)&gClientRect, (LPRECT)&gClientRect);
  473.         InvalidateRect(ghWnd, NULL, TRUE);
  474.         }
  475.             break;
  476.  
  477.         case SB_LINEDOWN:
  478.             if((CurrentIndex + IconsInWindow) < FileCount)
  479.                 {
  480.                 CurrentIndex += xMaxIcons;
  481.                 ScrollWindow(ghWnd, 0, -ySpacing,
  482.                   (LPRECT)&gClientRect, (LPRECT)&gClientRect);
  483.         InvalidateRect(ghWnd, NULL, TRUE);
  484.         }
  485.             break;
  486.  
  487.         case SB_PAGEUP:
  488.             if(CurrentIndex > 0)
  489.                 {
  490.                 if(CurrentIndex >= IconsInWindow)
  491.                     CurrentIndex -= IconsInWindow;
  492.                 else
  493.                     CurrentIndex = 0;
  494.                 InvalidateRect(ghWnd, NULL, TRUE);
  495.                 }
  496.             break;
  497.  
  498.         case SB_PAGEDOWN:
  499.             if((CurrentIndex + IconsInWindow) < FileCount)
  500.                 {
  501.                 CurrentIndex += IconsInWindow;
  502.                 InvalidateRect(ghWnd, NULL, TRUE);
  503.                 }
  504.             break;
  505.  
  506.         default:
  507.             break;
  508.         }
  509.     SetScrollPos(ghWnd, SB_VERT, (CurrentIndex > IconsInWindow ?
  510.       CurrentIndex+IconsInWindow : CurrentIndex), TRUE);
  511.     }
  512.  
  513. /*----------------------------------------------------------------------------*/
  514. /* FUNCTION: MakeFileList()                                                   */
  515. /*                                                                            */
  516. /* PURPOSE: Update lists of all directories and file names in a directory     */
  517. /*----------------------------------------------------------------------------*/
  518. void MakeFileList()
  519.     {
  520.     /* update file listbox and get number of files */
  521.     ListBox_ResetContent(hWndFile);
  522.     ListBox_Dir(hWndFile, 0x0000, "*.*");
  523.     FileCount = ListBox_GetCount(hWndFile);
  524.  
  525.     /* update dir listbox and get number of directories */
  526.     ListBox_ResetContent(hWndDir);
  527.     ListBox_Dir(hWndDir, 0x8010, "*.*");
  528.     DirCount = ListBox_GetCount(hWndDir);
  529.  
  530.     // add number of dirs to number of files to get total of displayed items
  531.     FileCount += DirCount;
  532.  
  533.     // update current selection parameters
  534.     CurrentIndex = 0;
  535.     CurrentSelection = 0;
  536.     xSelPos = 0;
  537.     ySelPos = 0;
  538.     AdjustScrollBar();
  539.     SetPathName();
  540.     }
  541.  
  542. /*----------------------------------------------------------------------------*/
  543. /* FUNCTION: MakeDriveList()                               */
  544. /*                                                                            */
  545. /* PURPOSE: Update the list of all drives  and make them menu items    */
  546. /*----------------------------------------------------------------------------*/
  547. void MakeDriveList()
  548.     {
  549.     int i;
  550.     char DriveName[20];
  551.  
  552.     ListBox_ResetContent(hWndDrive);
  553.     ListBox_Dir(hWndDrive, 0xC000, "*.*");
  554.     DriveCount = ListBox_GetCount(hWndDrive);
  555.  
  556.     for(i=0;i<DriveCount;i++)
  557.     {
  558.     ListBox_GetText(hWndDrive, i, DriveName);
  559.     AppendMenu(hMenu, MF_STRING, IDM_DRIVE+i, DriveName);
  560.     }
  561.     }
  562.  
  563. /*----------------------------------------------------------------------------*/
  564. /* FUNCTION: AdjustScrollBar()                                                */
  565. /*                                                                            */
  566. /* PURPOSE:  Sets vertical scroll bar thumbtrack indicator                    */
  567. /*----------------------------------------------------------------------------*/
  568. void AdjustScrollBar()
  569.     {
  570.     if(IconsInWindow < FileCount)
  571.         {
  572.         SetScrollRange(ghWnd, SB_VERT, 0, FileCount, FALSE);
  573.         SetScrollPos(ghWnd, SB_VERT, (CurrentIndex > IconsInWindow ?
  574.           CurrentIndex+IconsInWindow : CurrentIndex), TRUE);
  575.         }
  576.     else
  577.         ShowScrollBar(ghWnd, SB_VERT, 0);   /* hide scroll bar                */
  578.     }
  579.  
  580. /*----------------------------------------------------------------------------*/
  581. /* FUNCTION: ViewIcons(HDC hDC)                                               */
  582. /*                                                                            */
  583. /* PURPOSE:  Display the max number of icons that will fit in current window  */
  584. /*----------------------------------------------------------------------------*/
  585. void ViewIcons(HDC hDC)
  586.     {
  587.     int row;
  588.  
  589.     if(FileCount == 0)
  590.         return;
  591.  
  592.     for(row = 0; row < yMaxIcons ;row++)
  593.         ViewIconRow(hDC, row);
  594.     }
  595.  
  596. /*----------------------------------------------------------------------------*/
  597. /* FUNCTION: ViewIconRow(HDC hDC, int row)                                    */
  598. /*                                                                            */
  599. /* PURPOSE:  Display 1 row of icons and filenames                           */
  600. /*----------------------------------------------------------------------------*/
  601. void ViewIconRow(HDC hDC, int row)
  602.     {
  603.     int   i, index, Inverted;
  604.     int   xLoc = 4, yLoc = 0;
  605.     char  Name[16];
  606.     RECT  ClearRect, TextRect;
  607.  
  608.     yLoc += (ySpacing * row);
  609.     index = (CurrentIndex + (row * xMaxIcons));
  610.  
  611.     ClearRect.left   = xLoc;
  612.     ClearRect.top    = yLoc;
  613.     ClearRect.right  = xLoc + (xSpacing * xMaxIcons);
  614.     ClearRect.bottom = yLoc + ySpacing;
  615.     FillRect(hDC, (LPRECT)&ClearRect, GetStockObject(WHITE_BRUSH));
  616.  
  617.     for(i = 0; (i < xMaxIcons) && (index < FileCount) ;i++, index++)
  618.         {
  619.         if(index >= 0)
  620.         {
  621.         TextRect.left = xLoc;
  622.         TextRect.top = yLoc + 39;
  623.         TextRect.right = xLoc + xSpacing;
  624.         TextRect.bottom = yLoc + ySpacing;
  625.         if(index == CurrentSelection)
  626.         Inverted = 1;
  627.         else
  628.         Inverted = 0;
  629.         if(index < DirCount)
  630.         {
  631.         ListBox_GetText(hWndDir, index, Name);
  632.         ShowIcon(hDC, xLoc+(xSpacing/2-16), yLoc, Name, FOLDER);
  633.         DrawItemText(hDC, Name, TextRect, Inverted);
  634.         }
  635.         else
  636.         {
  637.         ListBox_GetText(hWndFile, index-DirCount, Name);
  638.         ShowIcon(hDC, xLoc+(xSpacing/2-16), yLoc, Name, FILENAME);
  639.         DrawItemText(hDC, Name, TextRect, Inverted);
  640.         }
  641.         }
  642.         xLoc += xSpacing;
  643.         }
  644.     }
  645.  
  646. /*----------------------------------------------------------------------------*/
  647. /*                                          */
  648. /* PURPOSE:  Display an icon                       */
  649. /*----------------------------------------------------------------------------*/
  650. int ShowIcon(HDC hDC, UINT xLoc, UINT yLoc, LPSTR FileName, int type)
  651.     {
  652.     HICON  hIcon;
  653.     char   DirIcon[256], Executable[256];
  654.     int length;
  655.  
  656.     if(type == FOLDER)
  657.     {
  658.     if(strstr(FileName, "system"))
  659.         {
  660.         DrawIcon(hDC, xLoc, yLoc+4, hSysFolderIcon);
  661.         return TRUE;
  662.         }
  663.  
  664.     if(strstr(FileName, "dos"))
  665.         {
  666.         DrawIcon(hDC, xLoc, yLoc+4, hDosFolderIcon);
  667.         return TRUE;
  668.         }
  669.  
  670.     GetRealDirName(FileName, Executable);
  671.     strcpy(DirIcon, PathName);
  672.     length = strlen(PathName);
  673.  
  674.     /* if dir isn't rootdir add "\" to dirname */
  675.     if(DirIcon[length-1] != '\\')
  676.         strcat(DirIcon, "\\");
  677.  
  678.     /* generate filename with same name as dir in directory dir */
  679.     strcat(DirIcon, Executable);
  680.     strcat(DirIcon, "\\");
  681.     strcat(DirIcon, Executable);
  682.     strcat(DirIcon, ".exe");
  683.  
  684.     /* try to find an icon, otherwise display normal folder icon */
  685.     if((hIcon = ExtractIcon(hInst, DirIcon, 0)) > 1)
  686.         {
  687.         DrawIcon(hDC, xLoc, yLoc+4, hIcon);
  688.         return TRUE;
  689.         }
  690.     DrawIcon(hDC, xLoc, yLoc+4, hFolderIcon);
  691.     return TRUE;
  692.     }
  693.  
  694.     /* if *.com file display dosfile icon */
  695.     if(strstr(FileName, ".com"))
  696.     {
  697.     DrawIcon(hDC, xLoc, yLoc+4, hDosFileIcon);
  698.     return TRUE;
  699.     }
  700.  
  701.     /* if *.bat file display dosfile icon */
  702.     if(strstr(FileName, ".bat"))
  703.     {
  704.     DrawIcon(hDC, xLoc, yLoc+4, hDosFileIcon);
  705.     return TRUE;
  706.     }
  707.  
  708.     /* PIF files have no icon and are not associated, so check if file */
  709.     /* if file is PIF file and display a PIF icon */
  710.     if(strstr(FileName, ".pif"))
  711.     {
  712.     DrawIcon(hDC, xLoc, yLoc+4, hPIFFileIcon);
  713.     return TRUE;
  714.     }
  715.  
  716.     /* check is file has an icon of it's own */
  717.     if((hIcon = ExtractIcon(hInst, FileName, 0)) > 1)
  718.     {
  719.     DrawIcon(hDC, xLoc, yLoc+4, hIcon);
  720.     return TRUE;
  721.     }
  722.  
  723.     /* if file doesn't have an icon but still is an *.exe file, display
  724.        EXE icon */
  725.     if(strstr(FileName, ".exe"))
  726.     {
  727.     DrawIcon(hDC, xLoc, yLoc+4, hExeFileIcon);
  728.     return TRUE;
  729.     }
  730.  
  731.     /* if *.dll file doesn't have an icon display normal file icon */
  732.     if(strstr(FileName, ".dll"))
  733.     {
  734.     DrawIcon(hDC, xLoc, yLoc+4, hFileIcon);
  735.     return TRUE;
  736.     }
  737.  
  738.     /* if file doesn't have an icon try to find an icon from it's
  739.        associated executable file, and diplay this icon with a small
  740.        file icon */
  741.     if(FindExecutable(FileName, PathName, (LPSTR) Executable) > 32)
  742.     if((hIcon = ExtractIcon(hInst, Executable, 0)) > 1)
  743.         {
  744.         DrawIcon(hDC, xLoc, yLoc+4, hIcon);
  745.         DrawIcon(hDC, xLoc, yLoc+4, hSmallIcon);
  746.         return TRUE;
  747.         }
  748.  
  749.     /* if file is *.386 file but doesn't have an icon display an 386 icon */
  750.     if(strstr(FileName, ".386"))
  751.     {
  752.     DrawIcon(hDC, xLoc, yLoc+4, hEnhIcon);
  753.     return TRUE;
  754.     }
  755.  
  756.     /* display special icons for type 1 font files */
  757.     if(strstr(FileName, ".pfb"))
  758.     {
  759.     DrawIcon(hDC, xLoc, yLoc+4, hATMFileIcon);
  760.     return TRUE;
  761.     }
  762.  
  763.     if(strstr(FileName, ".pfm"))
  764.     {
  765.     DrawIcon(hDC, xLoc, yLoc+4, hATMFileIcon);
  766.     return TRUE;
  767.     }
  768.  
  769.     if(strstr(FileName, ".afm"))
  770.     {
  771.     DrawIcon(hDC, xLoc, yLoc+4, hATMFileIcon);
  772.     return TRUE;
  773.     }
  774.  
  775.     /* display special TrueType icon for Truetype font files */
  776.     if(strstr(FileName, ".ttf"))
  777.     {
  778.     DrawIcon(hDC, xLoc, yLoc+4, hTTFFileIcon);
  779.     return TRUE;
  780.     }
  781.  
  782.     if(strstr(FileName, ".fot"))
  783.     {
  784.     DrawIcon(hDC, xLoc, yLoc+4, hTTFFileIcon);
  785.     return TRUE;
  786.     }
  787.  
  788.    /* display an font icon for normal font files */
  789.    if(strstr(FileName, ".fnt"))
  790.     {
  791.     DrawIcon(hDC, xLoc, yLoc+4, hFontIcon);
  792.     return TRUE;
  793.     }
  794.  
  795.     if(strstr(FileName, ".fon"))
  796.     {
  797.     DrawIcon(hDC, xLoc, yLoc+4, hFontIcon);
  798.     return TRUE;
  799.     }
  800.  
  801.     /* display an DRV icon for *.drv driver files */
  802.     if(strstr(FileName, ".drv"))
  803.     {
  804.     DrawIcon(hDC, xLoc, yLoc+4, hDrvFileIcon);
  805.     return TRUE;
  806.     }
  807.  
  808.     /* finally display an FILE icon for all other files */
  809.     DrawIcon(hDC, xLoc, yLoc+4, hFileIcon);
  810.     return TRUE;
  811.     }
  812.  
  813. /***********************************************************************
  814.  * Title      : DrawItemText
  815.  *                                                                            
  816.  * Parameters : hDC - handle to the DeviceContext
  817.  *                    We'll need this for DrawText() later.                    
  818.  *              wIndex - index to the ITEMSTRUCT structure
  819.  *              bInvert- whether or not the text is highlighted
  820.  *                                                             
  821.  * Description: Draws the text under the icon
  822.  **********************************************************************/
  823. void DrawItemText (HDC hDC, LPSTR Text, RECT textrect, int Invert)
  824. {
  825.    COLORREF   crBkColor, crTxtColor;
  826.  
  827.    if(Invert)
  828.        {
  829.        crBkColor = SetBkColor(hDC, GetSysColor(COLOR_HIGHLIGHT));
  830.        crTxtColor = SetTextColor(hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
  831.  
  832.        DrawText(hDC, Text, lstrlen (Text), &textrect, DT_CENTER);
  833.  
  834.        SetBkColor(hDC, crBkColor );
  835.        SetTextColor(hDC, crTxtColor); 
  836.        }
  837.    else
  838.        DrawText(hDC, Text, lstrlen (Text), &textrect, DT_CENTER);
  839. }
  840.  
  841.  
  842. /**********************************************************************
  843. UpdateCurrentText(): function is used to display the highlighted text
  844.              of the icon that is selected by the user, and to
  845.              display normally the text or the previous selection
  846. ***********************************************************************/
  847. void UpdateCurrentText (int Invert)
  848. {
  849.    COLORREF   crBkColor, crTxtColor;
  850.    HFONT hOldFont;
  851.    HDC hDC;
  852.    RECT TextRect;
  853.    int xLoc, yLoc;
  854.    char Text[20];
  855.  
  856.    if(CurrentSelection < DirCount)
  857.     ListBox_GetText(hWndDir, CurrentSelection, Text);
  858.    else
  859.     ListBox_GetText(hWndFile, CurrentSelection-DirCount, Text);
  860.  
  861.    hDC = GetDC(ghWnd);
  862.    hOldFont = SelectObject(hDC, GetStockObject (ANSI_VAR_FONT));
  863.  
  864.    xLoc = xSelPos * xSpacing+4;
  865.    yLoc = ySelPos * ySpacing;
  866.  
  867.    TextRect.left = xLoc;
  868.    TextRect.top = yLoc + 39;
  869.    TextRect.right = xLoc + xSpacing;
  870.    TextRect.bottom = yLoc + ySpacing;
  871.  
  872.    if(Invert)
  873.        {
  874.        crBkColor = SetBkColor  (hDC, GetSysColor (COLOR_HIGHLIGHT));
  875.        crTxtColor= SetTextColor(hDC, GetSysColor (COLOR_HIGHLIGHTTEXT)); 
  876.        DrawText (hDC, Text, lstrlen(Text), &TextRect, DT_CENTER);
  877.        SetBkColor  (hDC, crBkColor );
  878.        SetTextColor(hDC, crTxtColor); 
  879.        }
  880.    else
  881.        DrawText (hDC, Text, lstrlen(Text), &TextRect, DT_CENTER);
  882.  
  883.    SelectObject(hDC, hOldFont);
  884.    ReleaseDC(ghWnd, hDC);
  885. }
  886.  
  887. /*---------------------------------------------------------------------*/
  888. /* GetREalDirName(): removes the [ ] characters from the directoryname */
  889. /*---------------------------------------------------------------------*/
  890. void GetRealDirName(char *Name, char *DirName)
  891.     {
  892.     int i, length;
  893.  
  894.     length=strlen(Name);
  895.  
  896.     for(i=0;i<length-2;i++)
  897.     DirName[i] = Name[i+1];
  898.     DirName[i] = '\0';
  899.     }
  900.  
  901. /*----------------------------------------------------------------------------*/
  902. /* SetPathName(): Updates the Window caption with the current drive and path */
  903. /*---------------------------------------------------------------------------*/
  904. void SetPathName()
  905.     {
  906.     strcpy(PathName, getcwd(PathName, 90));
  907.     SetWindowText(ghWnd, (LPSTR) PathName);
  908.     }
  909.  
  910. /****************************************************************************/
  911. /**********************                            **************************/
  912. /********************** DROP-FILE SERVER FUNCTIONS **************************/
  913. /**********************                            **************************/
  914. /****************************************************************************/
  915.  
  916. typedef struct {
  917.         WORD  wSize;                            // Size of data structure
  918.         POINT ptMousePos;                       // Position of mouse cursor
  919.         BOOL  fInNonClientArea;                 // Was the mouse in the
  920.                                                 // window's non-client area
  921. } DROPFILESTRUCT, FAR *LPDROPFILESTRUCT;
  922.  
  923.  
  924. HANDLE FAR DragCreateFiles (LPPOINT lpptMousePos,
  925.                            BOOL fInNonClientArea)
  926. {
  927.  
  928.     HGLOBAL hDrop;
  929.     LPDROPFILESTRUCT lpDropFileStruct;
  930.  
  931.     // GMEM_SHARE must be specified because the block will
  932.     // be passed to another application
  933.     hDrop = GlobalAlloc(GMEM_SHARE | GMEM_MOVEABLE | GMEM_ZEROINIT,
  934.                         sizeof(DROPFILESTRUCT) + 1);
  935.  
  936.     // If unsuccessful, return NULL
  937.     if (hDrop == NULL) return(hDrop);
  938.  
  939.     // Lock block and initialize the data members
  940.     lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
  941.     lpDropFileStruct->wSize = sizeof(DROPFILESTRUCT);
  942.     lpDropFileStruct->ptMousePos = *lpptMousePos;
  943.     lpDropFileStruct->fInNonClientArea = fInNonClientArea;
  944.     GlobalUnlock(hDrop);
  945.     return(hDrop);
  946. }
  947.  
  948.  
  949. HANDLE FAR DragAppendFile (HGLOBAL hDrop, LPCSTR szPathname)
  950. {
  951.     LPDROPFILESTRUCT lpDropFileStruct;
  952.     LPCSTR lpCrnt;
  953.     WORD wSize;
  954.  
  955.     lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
  956.  
  957.     // Point to first pathname in list
  958.     lpCrnt = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
  959.  
  960.     // Search for a pathname were first byte is a zero byte
  961.     while (*lpCrnt)         // While the 1st char of path is non-zero
  962.        {
  963.        while (*lpCrnt) lpCrnt++;   // Skip to zero byte
  964.        lpCrnt++;
  965.        }
  966.  
  967.     // Calculate current size of block
  968.     wSize = (WORD) (lpCrnt - (LPSTR) lpDropFileStruct + 1);
  969.     GlobalUnlock(hDrop);
  970.  
  971.     // Increase block size to accommodate new pathname being appended
  972.     hDrop = GlobalReAlloc(hDrop, wSize + lstrlen(szPathname) + 1,
  973.                           GMEM_MOVEABLE | GMEM_ZEROINIT | GMEM_SHARE);
  974.  
  975.     // Return NULL if insufficient memory
  976.     if (hDrop == NULL) return(hDrop);
  977.  
  978.     lpDropFileStruct = (LPDROPFILESTRUCT) GlobalLock(hDrop);
  979.     // Append the pathname to the block
  980.     lstrcpy((LPSTR) lpDropFileStruct + wSize - 1, szPathname);
  981.     GlobalUnlock(hDrop);
  982.     return(hDrop);  // Return the new handle to the block
  983. }
  984.